package com.stx.bms.web;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.stx.bms.entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/json")
public class JsonTestServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request,
                         HttpServletResponse response)
            throws ServletException, IOException {


        User user = new User();
        user.setUsername("123");
        user.setPassword("kkk");

        //封装成JSON
        ObjectMapper mapper = new ObjectMapper();

        String str = mapper.writeValueAsString(user);
        System.out.println(str);

        response.getWriter().write(str);
        response.getWriter().close();

    }
}
